<?php




namespace App\Http\Controllers\Api\Dynamic;


use App\Http\Controllers\Api\BasicController;
use App\Http\Controllers\Api\User\MarketLevel;
use App\Models\Dynamic\Likes;
use App\Models\Dynamic\Lists;
use App\Models\Site\SiteCode;
use App\Models\UserInfo;
use App\Services\Sms;
use GatewayWorker\Lib\Gateway;
use Jiannei\Response\Laravel\Support\Facades\Response;

class DynamicController extends BasicController
{



    /**
     * @api {get} /dynamic/add  发帖
     * @apiName UserInfoBasic
     * @apiDescription
     *
     * @apiHeader {String} Authorization='Bearer '
     */
    public function add()
    {
        $data = $this->data(['title', 'tag_ids','media_type','media_files','visible_range']);
        $data['tag_ids'] = implode(',',$data['tag_ids']);
        $data['media_files'] = implode(',',$data['media_files']);
        $data['user_id'] = $this->uid();
        $result = Lists::create($data);
        return Response::success($result);
    }

    /**
     * @api {post} /dynamic/update  编辑
     * @apiName UserInfoBasic
     * @apiHeader {String} Authorization='Bearer '
     */
    public function update()
    {
        $data = $this->data(['title', 'tag_ids','media_type','media_files','visible_range','status']);
        $where=['id'=>$this->request->input('id'),'user_id'=>$this->uid()];
        $data['tag_ids'] = implode(',',$data['tag_ids']);
        $data['media_files'] = implode(',',$data['media_files']);
        $result = Lists::where($where)->update($data);
        return Response::success($result);
    }

    /**
     * @api {get} /dynamic/del  删除
     * @apiName UserInfoBasic
     * @apiHeader {String} Authorization='Bearer '
     */
    public function del()
    {
        $data = $this->data(['title', 'tag_ids','media_files','visible_range','status']);
        $where=['id'=>$this->request->input('id'),'user_id'=>$this->uid()];
        $result = Lists::query()->where($where)->delete();
        return Response::success($result);
    }

    public function list(){
        $uid=$this->uid();
        $result= Lists::getList($uid);
        return Response::success(['data'=>$result]);
    }

    public function myList(){
        $where['user_id']=$this->uid();
        $result= Lists::where($where)->paginate(10);
        return Response::success($result);
    }

    public function theirList(){
        $their_id=$this->request->input('uid');
        $uid=$this->uid();
        $result= Lists::getTheirList($uid,$their_id);
        return Response::success($result);
    }

    public function likes(){
        $post_id=$this->request->input('post_id');
        $type=$this->request->input('type');
        $is_like=$this->request->input('is_like',1);
        if($type!=3){
            $post=Lists::find($post_id);
            if(!$post)
                return Response::success([],'帖子不存在');
        }
        $data = $this->data(['type','post_id']);
        $data['user_id'] = $this->uid();
        $like=Likes::where($data)->first();

        if(!$like && $is_like==1){
            $result=Likes::create($data);

            (new Likes())->upUserNum($post);
        }else{
            if($like && $is_like==0){
                $like->delete();
                if($data['type']==1)
                (new Likes())->deUserNum($post);
            }
        }
        return Response::success($result ?? []);
    }


    public function likesList(){
        $type=$this->request->input('type',2);
        $page=$this->request->input('page',1);
        $ids=Likes::where(['user_id'=>$this->uid(),'type'=>$type])->orderByDesc('id')->forPage($page)->pluck('post_id');
        $result= (new Lists())->collectList($ids,$this->uid());
        return Response::success($result);

    }
}
